//
//  Tag+CoreDataClass.swift
//  Drop
//
//  Created by hushaohua on 2017/12/19.
//  Copyright © 2017年 init. All rights reserved.
//
//

import Foundation
import CoreData

@objc(Tag)
public class Tag: NSManagedObject {
    static func newInstanceFrom(_ context:NSManagedObjectContext) -> Tag{
        let tag:Tag = NSEntityDescription.insertNewObject(forEntityName: "Tag", into: context) as! Tag;
        tag.context = context;
        tag.createdDate = Date() as NSDate;
        return tag;
    }
    
    static func defaultFetchRequest() -> NSFetchRequest<Tag>{
        return NSFetchRequest(entityName: "Tag");
    }
    
    static func defaultSort() -> NSSortDescriptor{
        return NSSortDescriptor(key: "createdDate", ascending: false);
    }
    
    static func dateSorts() -> [NSSortDescriptor]{
        let createdSort = NSSortDescriptor(key: "createdDate", ascending: false);
        let updatedSort = NSSortDescriptor(key: "lastUpdatedDate", ascending: false);
        return [updatedSort, createdSort];
    }
    
    var context:NSManagedObjectContext?;
    static func newInstance() -> Tag{
        let context : NSManagedObjectContext = HStoreManager.defaultManger().context;
        let tag:Tag = NSEntityDescription.insertNewObject(forEntityName: "Tag", into: context) as! Tag;
        tag.createdDate = Date() as NSDate;
        tag.context = context;
        return tag;
    }
    
    func deleteFromContext() -> Void{
        let context : NSManagedObjectContext = HStoreManager.defaultManger().context;
        context.delete(self);
    }
    
    func updateName(_ name:String){
        self.name = name;
    }
    
    func updateName(_ name:String, isEn:Bool){
        if isEn {
            self.name = name;
        }else{
            self.enName = name;
        }
    }
}
